//
//  KuangJiVC.m
//  digitalCurrency
//
//  Created by 小震GG on 2019/10/30.
//  Copyright © 2019 XinHuoKeJi. All rights reserved.
//

#import "KuangJiVC.h"
#import "SDCycleScrollView.h"
#import "PersonPackageCell.h"
#import "MyKuangJiVC.h"
#import "NewAllNetManager.h"

@interface KuangJiVC ()<SDCycleScrollViewDelegate,UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

@property (nonatomic, strong) SDCycleScrollView *cycleScrollView;
@property (nonatomic, strong) UITableView *tableViewList;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) NSMutableArray *bannerArr;
@end

@implementation KuangJiVC

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self loadData];
    [self loadBannerData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.viewTitle.text = LocalizationKey(@"矿机商城");
    
    self.dataArr = [[NSMutableArray alloc]init];
    self.bannerArr = [[NSMutableArray alloc]init];
    [self setTableView];
    [self setBannerView];
}

- (void)loadData{
   [NewAllNetManager kuangJiList:@"1" pageNumStr:@"1" pageSizeStr:@"20" CompleteHandle:^(id resPonseObj, int code) {
         if (code) {
             NSInteger codeInt = [resPonseObj[@"code"] intValue];
             if (codeInt == 0) {
                 self.dataArr = [[NSMutableArray alloc]init];
                 self.dataArr = resPonseObj[@"data"][@"list"];
             }
             [self.tableViewList reloadData];
         }
     }];
}

- (void)loadBannerData{
    [NewAllNetManager kuangJiBanner:@"" CompleteHandle:^(id resPonseObj, int code) {
        if (code) {
            NSInteger codeInt = [resPonseObj[@"code"] intValue];
            if (codeInt == 0) {
                self.bannerArr = [[NSMutableArray alloc]init];
                self.bannerArr = resPonseObj[@"data"][@"banner"];
                
                [self.cycleScrollView removeFromSuperview];
                self.cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(16, 20, kWindowW-32, (162.0/375.0)*kWindowW) shouldInfiniteLoop:YES imageNamesGroup:self.bannerArr];
                self.cycleScrollView.layer.masksToBounds = YES;
                self.cycleScrollView.layer.cornerRadius = 6.0;
                self.cycleScrollView.delegate = self;
                self.cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleNone;
                [self.view addSubview:self.cycleScrollView];
                self.cycleScrollView.scrollDirection = UICollectionViewScrollDirectionHorizontal;
                
            }
            [self.tableViewList reloadData];
        }
    }];
}

- (void)setTableView{
    [self.view addSubview:self.tableViewList];
    [self.tableViewList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo((162.0/375.0)*kWindowW+95);
        make.left.equalTo(0);
        make.right.equalTo(0);
        make.bottom.equalTo(0);
    }];
    if (@available(iOS 11.0, *)) {
        self.tableViewList.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        self.tableViewList.estimatedRowHeight = 0;
        self.tableViewList.estimatedSectionHeaderHeight = 0;
        self.tableViewList.estimatedSectionFooterHeight = 0;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

- (void)setBannerView{
    [self.cycleScrollView removeFromSuperview];
    self.cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(16, 20, kWindowW-32, (162.0/375.0)*kWindowW) shouldInfiniteLoop:YES imageNamesGroup:self.bannerArr];
    self.cycleScrollView.layer.masksToBounds = YES;
    self.cycleScrollView.layer.cornerRadius = 6.0;
    self.cycleScrollView.delegate = self;
    self.cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleNone;
    [self.view addSubview:self.cycleScrollView];
    self.cycleScrollView.scrollDirection = UICollectionViewScrollDirectionHorizontal;
          //    cycleScrollView.autoScrollTimeInterval = 4.0;
//    UIImageView *bannerImage = [[UIImageView alloc]init];
//    [self.cycleScrollView addSubview:bannerImage];
//    [bannerImage mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.top.right.bottom.equalTo(0);
//    }];
//    bannerImage.image = ImageName(@"k_banner");
    
    UIView *headLineView = [[UIView alloc]init];
    [self.view addSubview:headLineView];
    [headLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(0);
        make.top.equalTo((162.0/375.0)*kWindowW+33);
        make.height.equalTo(9);
    }];
    headLineView.backgroundColor = [UIColor colorWithHEX:@"#F5F5F5"];
    
    UIView *titleView = [[UIView alloc]init];
    [self.view addSubview:titleView];
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(0);
        make.top.equalTo((162.0/375.0)*kWindowW+64);
        make.height.equalTo(30);
    }];
    
    UILabel *titleLB = [[UILabel alloc]init];
    [titleView addSubview:titleLB];
    [titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(25);
        make.top.equalTo(0);
    }];
    titleLB.text = LocalizationKey(@"商品分类");
    titleLB.textColor = tMainColor;
    
    UIView *shuView = [[UIView alloc]init];
    [titleView addSubview:shuView];
    [shuView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(15);
        make.centerY.equalTo(titleLB);
        make.width.equalTo(2);
        make.height.equalTo(9);
    }];
    shuView.backgroundColor = tMainColor;
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 123;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PersonPackageCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"id"];
    if (cell == nil) {
        cell = [[PersonPackageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"id"];
    }
    
    [cell upDataModel:self.dataArr[indexPath.row]];
    
    return cell;
   
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (UITableView*)tableViewList
{
    if (_tableViewList == nil) {
        _tableViewList = [[UITableView alloc]init];
        _tableViewList.delegate = self;
        _tableViewList.dataSource = self;
        _tableViewList.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableViewList.backgroundColor = [UIColor clearColor];
        _tableViewList.showsVerticalScrollIndicator = NO;
        _tableViewList.sectionFooterHeight = 0;
    }
    return _tableViewList;
}

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"---点击了第%ld张图片", (long)index);
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
